<?php

namespace app\mp\controller;

use app\common\entity\BackResult;
use app\common\entity\Result;
use app\common\model\UserStudy;
use app\common\model\VideoList;
use app\common\service\StudyPlanService;
use app\common\model\Order as OrderModel;
use app\common\model\StudyPlan as StudyPlanModel;
use app\common\service\UserService;
use app\common\service\VideoService;
use app\Request;

class StudyPlan extends Base
{
    /**
     * 查询列表
     * @param Request $request
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $page = (int)$request->param('page', 1);
        $size = (int)$request->param('size', 10);
        $service = new StudyPlanService();
        $where = ['uid' => $this->user['id'], 'type' => StudyPlanModel::TOPIC];

        $list = $service->getList($page, $size, $where);
        return Result::success([
            'list' => $list['list'],
            'total' => [
                'studyTime' => UserService::getExtValue($this->user['id'], UserService::TYPE_EXT_TOTAL_STUDY_TIME) ?: 0,
                'studyCompleted' => UserService::getExtValue($this->user['id'], UserService::TYPE_EXT_TOTAL_STUDY_COMPLETED) ?: 0,
                'onlineTime' => time() - strtotime($this->user['login_time']),
                'todayTime' => VideoService::getTodayStudyTime($this->user['id'])
            ]
        ]);
    }

    /**
     * 加入到我的学习计划
     * @param Request $request
     * @return \think\Response
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function add(Request $request)
    {
        $srcId = $request->param('src_id');
        $type = $request->param('type');
        // 已加入学习计划时直接返回
        if (StudyPlanService::get($srcId, $type, $this->user['id'])) {
            return BackResult::success(true);
        }
        // 判断是否为VIP
        if (UserService::getVipInfo($this->user['id'])) {
            // vip用户直接加入
            StudyPlanService::add($srcId, $type, $this->user['id']);
        } else {
            // 非vip用户则查询购买订单
            $order = OrderModel::where([
                'uid' => $this->user['id'],
                'type' => OrderModel::TOPIC,
                'src_id' => $srcId,
                'status' => 1
            ])->find();
            if ($order) {
                // 订单存在并且已支付成功，加入
                StudyPlanService::add($srcId, $type, $this->user['id'], $order->id);
            } else {
                // 否则提示支付
                return BackResult::success(false);
            }
        }
        return BackResult::success(true);
    }

    /**
     * 查看是否已加入我的学习计划
     * @param Request $request
     * @return \think\Response
     */
    public function get(Request $request)
    {
        $srcId = $request->param('src_id');
        $type = $request->param('type');
        $result = StudyPlanService::get($srcId, $type, $this->user['id']);
        return BackResult::success(!!$result);
    }

    public function del(Request $request)
    {
        $id = $request->param('id');
        if (!$id || !($plan = StudyPlanModel::find($id)) || $plan->uid != $this->user['id']) {
            return BackResult::error(null, '参数异常');
        }
        if ($plan->order_id > 0) {
            return BackResult::error(null, '付费购买的学习计划不能删除');
        }
        // 删除课程
        $plan->delete();
        // 删除课程下所有视频
        $videoIds = VideoList::where('topic_id', $plan->src_id)->column('id', 'id');
        UserStudy::where('video_id', 'in', $videoIds)->delete();
        return BackResult::success();
    }

    public function getMyTrade(Request $request)
    {
        $type = $request->param('type');
        $page = (int)$request->param('page', 1);

        $types = ['video' => 1, 'topic' => 2];
        if (!isset($types[$type])) {
            return BackResult::error(null, '参数异常');
        }

        return BackResult::success(StudyPlanService::getTrade($this->user['id'], $types[$type], $page));
    }
}